package com.csair.common.helper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.csair.common.CommonApplication;
import com.csair.common.utils.LogUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class BasicFunctions {

    /**
     * 获取字符串资源
     */
    public static String text(@StringRes int id, Object... params) {
        return CommonApplication.getApplication().getString(id, params);
    }

    /**
     * 检查某个权限是否被授予
     */
    public static boolean checkPermission(String permission) {
        Context context = CommonApplication.getApplication();
        PackageManager pm = context.getPackageManager();
        String packageName = context.getPackageName();
        return (PackageManager.PERMISSION_GRANTED == pm.checkPermission(permission, packageName));
    }

    public static void requestPermission(Activity activity, String permission, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(new String[]{permission}, requestCode);
        }
    }

    /**
     * 调用打电话
     */
    public static void call(Activity activity, String phoneNo) {
        Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNo));
        PackageManager pm = activity.getPackageManager();
        if (i.resolveActivity(pm) != null) {
            try {
                activity.startActivity(i);
            } catch (Exception ex) {
                i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNo));
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    activity.startActivity(i);
                } catch (Exception e) {
                    LogUtils.printStackTrace(e);
                }
            }
        }
    }

    /**
     * 隐藏键盘，其中传入参数view不能为null
     */
    public static void hideSoftInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private static Bundle getMetaData() {
        try {
            ApplicationInfo ai = CommonApplication.getApplication().getPackageManager().getApplicationInfo(
                    CommonApplication.getApplication().getPackageName(), PackageManager.GET_META_DATA);
            return ai.metaData;
        } catch (Exception e) {
            LogUtils.printStackTrace(e);
        }
        return null;
    }

    public static boolean getMetaBoolean(String name) {
        Bundle metaData = getMetaData();
        return metaData != null && metaData.getBoolean(name, false);
    }

    public static int getMetaInt(String name) {
        Bundle metaData = getMetaData();
        return metaData == null ? 0 : metaData.getInt(name, 0);
    }

    public static String getMetaString(String name) {
        Bundle metaData = getMetaData();
        return metaData == null ? "" : metaData.getString(name);
    }

    public static void editorActionListener(EditText et, IEditorAction iEditorAction) {
        et.setOnEditorActionListener((view, actionId, event) -> {
            if (iEditorAction != null && (actionId == EditorInfo.IME_ACTION_GO ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_SEND)) {
                iEditorAction.editorAction();
                return true;
            }
            return false;
        });
    }

    public static int getStatusBarHeight() {
        int result = 0;
        int resourceId = CommonApplication.getApplication().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = CommonApplication.getApplication().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public interface IEditorAction {
        void editorAction();
    }



    public static int dp2px(Context context, float dp) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return (int) (dm.density * dp + 0.5f);
    }

    public static int px2dp(Context context, float px) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                px, context.getResources().getDisplayMetrics());
    }


}
